package br.com.viewcar.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "USUARIO", schema = "CHECKROTA")
@NamedQueries({
	@NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u WHERE u.status = 1"),
	@NamedQuery(name = "Usuario.findByCliente", query = "SELECT u FROM Usuario u WHERE u.cliente.id = ? AND u.status = 1"),
	@NamedQuery(name = "Usuario.findByMail", query = "SELECT u FROM Usuario u WHERE u.emailUsuario = ? AND u.status = 1")
})
public class Usuario implements Serializable {

	private static final long serialVersionUID = -6274519554954267290L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="ID", nullable = false)
	private Integer id;

	@Temporal(TemporalType.DATE)
	@Column(name="DATA_CADASTRO", nullable = false)
	private Date dataCadastro;

	@Column(name="NOME", nullable = false, length = 50)
	private String nome;

	@Column(name="EMAIL", nullable = true, length = 100)
	private String emailUsuario;

	@Column(name="LOGIN", nullable = false, length = 15)
	private String login;

	@Column(name="PASSWORD", nullable = false, length = 15)
	private String senha;

	@Column(name="PASSWORD_CORTE", nullable = true, length = 15)
	private String senhaCorte;

	@Column(name="STATUS", nullable = false)
	private Boolean status;

	@ManyToOne
	@JoinColumn(name="CLIENTE_ID")
	private Cliente cliente;

	@Fetch(FetchMode.SUBSELECT)
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "USUARIO_GRUPO", schema = "VIEWCAR", 
			joinColumns=@JoinColumn(name="ID_USUARIO"), inverseJoinColumns=@JoinColumn(name="ID_GRUPO"))
	private List<Grupo> grupos;

	@Fetch(FetchMode.SUBSELECT)
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name="USUARIO_PERMISSAO", schema = "VIEWCAR", 
			joinColumns=@JoinColumn(name="ID_USUARIO"), inverseJoinColumns=@JoinColumn(name="ID_PERMISSAO"))
	private List<Permissao> permissoes;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmailUsuario() {
		return emailUsuario;
	}

	public void setEmailUsuario(String emailUsuario) {
		this.emailUsuario = emailUsuario;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public String getSenhaCorte() {
		return senhaCorte;
	}

	public void setSenhaCorte(String senhaCorte) {
		this.senhaCorte = senhaCorte;
	}

	public Boolean getStatus() {
		return status;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public List<Grupo> getGrupos() {
		return grupos;
	}

	public void setGrupos(List<Grupo> grupos) {
		this.grupos = grupos;
	}

	public List<Permissao> getPermissoes() {
		return permissoes;
	}

	public void setPermissoes(List<Permissao> permissoes) {
		this.permissoes = permissoes;
	}

	public int hashCode() {

		int result = 1;
		result = 31 * result + ((id == null) ? 0 : id.hashCode());
		result = 31 * result + ((nome == null) ? 0 : nome.hashCode());
		result = 31 * result + ((senha == null) ? 0 : senha.hashCode());

		return result;
	}

	public boolean equals(Object obj) {

		if (this == obj)
			return true;

		if (obj == null)
			return false;

		if (getClass() != obj.getClass())
			return false;

		Usuario other = (Usuario) obj;

		return ((id == null && other.id == null) || (id != null && id.equals(other.id))) &&
			((nome == null && other.nome == null) || (nome != null && nome.equals(other.nome))) &&
			((senha == null && other.senha == null) || (senha != null && senha.equals(other.senha)));
	}
}